/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddInventoryDialog.java
 *
 * Created on Apr 7, 2011, 11:07:02 PM
 * 
 */

package client;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import org.workplicity.inventorycontrol.entry.Inventory;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

/**
 *
 * @author Neal
 * @author Brian Gormanly
 */
public class AddInventoryDialog extends javax.swing.JDialog
{
    private boolean addedInventory = false;
    private static Inventory thisInventory = new Inventory("");

    /** Creates new form AddInventoryDialog */
    public AddInventoryDialog(java.awt.Frame parent, Inventory inventory, boolean modal)
    {
        super(parent, modal);

        // Attempt to set the appearance to the system default
        try
        {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
            this.thisInventory = inventory;
        }
        catch (Exception ex)
        {
            System.out.println("Failed to set the look and feel.");
        }

        initComponents();
        init();
    }

    private void init()
    {
        // Get the screen size object
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();

        // Get the size of the screen
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;

        // Get the size of the current window
        int windowHeight = this.getHeight();
        int windowWidth = this.getWidth();

        int newX = (screenWidth / 2) - (windowWidth / 2);
        int newY = (screenHeight / 2) - (windowHeight / 2);
        this.setLocation(newX, newY);

        this.setTitle("Add New Inventory");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        inventoryNameTextField = new javax.swing.JTextField();
        inventoryDescriptionTextField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(client.MainFrame.class).getContext().getResourceMap(AddInventoryDialog.class);
        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        inventoryNameTextField.setText(resourceMap.getString("inventoryNameTextField.text")); // NOI18N
        inventoryNameTextField.setName("inventoryNameTextField"); // NOI18N

        inventoryDescriptionTextField.setText(resourceMap.getString("inventoryDescriptionTextField.text")); // NOI18N
        inventoryDescriptionTextField.setName("inventoryDescriptionTextField"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inventoryNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                            .addComponent(inventoryDescriptionTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(inventoryNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(inventoryDescriptionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        
        String errorMessage = "";
        
        String nameEntered = inventoryNameTextField.getText();
        String descriptionEntered = inventoryDescriptionTextField.getText();
        
        if(nameEntered.equals("") || nameEntered.equals("Inventory name")) {
            errorMessage += "Plaese enter a valid name\n";
        }
        if(descriptionEntered.equals("") || descriptionEntered.equals("Inventory description")) {
            errorMessage += "Please enter a Description\n";
        }
        
        
        if(errorMessage.length() > 0) {
            JOptionPane.showMessageDialog(this,
                        errorMessage,
                        "Required parameters missing",
                        JOptionPane.ERROR_MESSAGE);
        }
        else {
            Inventory thisInventory = new Inventory(nameEntered);
            thisInventory.setName(nameEntered);
            thisInventory.setDescription(descriptionEntered);
            insertInventory(thisInventory);

            dispose();
        }
}//GEN-LAST:event_saveButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.setVisible(false);
        this.addedInventory = false;
}//GEN-LAST:event_cancelButtonActionPerformed

    public boolean addedInventory()
    {
        return this.addedInventory;
    }

    
    private void insertInventory(Inventory inventory){

        final AddInventoryDialog frame = this;
        WorkletContext context = WorkletContext.getInstance();

         System.out.println(inventory.getId());

         if (!Helper.insert(inventory, "Inventories", context)) {
             JOptionPane.showMessageDialog(frame, "insert Inventory failed!",
                "Inventories", JOptionPane.ERROR_MESSAGE);

         }// end if

         System.out.println(inventory.getId());

    }// end insert

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AddInventoryDialog dialog = new AddInventoryDialog(new javax.swing.JFrame(), thisInventory, true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField inventoryDescriptionTextField;
    private javax.swing.JTextField inventoryNameTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

}
